<!--
 * @Author: your name
 * @Date: 2022-03-28 20:17:00
 * @LastEditTime: 2024-05-23 16:31:14
 * @LastEditors: smallWhite
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /huizhou_website_admin/src/views/project_menagement/components/modal.vue
-->
<template>
  <el-dialog :title="title"
    :visible.sync="dialogVisible"
    width="1024px"
    :before-close="handleClose">
    <el-form ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="90px"
      class="demo-ruleForm"
      style="width:100%">
      <el-form-item label="分类"
        prop="projectId">
        <el-select
          v-model="ruleForm.projectId"
          style="width:100%"
          placeholder="请选择分类"
          @change="changeProject">
          <el-option
            v-for="(item,index) in list"
            :key="index"
            :label="item.name"
            :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item
        label="业务名称"
        prop="name">
        <el-input
          v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item
        label="业务图片"
        prop="url">
        <el-upload
          class="avatar-uploader"
          :action="action"
          :show-file-list="false"
          name="multipartFile"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img
            v-if="ruleForm.imagesUrl"
            :src="ruleForm.imagesUrl"
            class="avatar">
          <i v-else
            class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <el-input
          v-show="false"
          v-model="ruleForm.url"
          placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="排序"
        prop="sort">
        <el-input
          v-model="ruleForm.sort"
          placeholder="请输入排序" />
      </el-form-item>
      <el-form-item
        label="是否启用"
        prop="isEnable">
        <el-switch
          v-model="ruleForm.isEnable"
          :active-value="0"
          :inactive-value="1" />
      </el-form-item>
      <el-form-item
        label="业务详情"
        prop="content">

        <!-- <Editors ref="Editor"
          v-model="ruleForm.content"
          :conact="ruleForm.content"
          class="CKEditor"
          @change="callbackChangeEditor" /> -->

        <Editor ref="Editor"
          v-model="ruleForm.content"
          :content="ruleForm.content"
          class="CKEditor"
          @change="callbackChangeEditor" />
        <!-- <el-input v-show="true" v-model="ruleForm.content" type="textarea" placeholder="请输入介绍" /> -->
      </el-form-item>
    </el-form>
    <span slot="footer"
      class="dialog-footer">
      <el-button
        @click="resetForm('ruleForm')">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { REQUEST_LIST } from '@/api/requestList'
import { HTTP_URI } from '@/api/baseUrl'
export default {
  data() {
    return {
      action: HTTP_URI.UPLOADIMG,
      dialogVisible: false,
      title: '',
      url: '',
      list: [],
      ruleForm: {
        projectId: '',
        name: '',
        projectParentId: '',
        projectName: '',
        sort: '',
        content: '',
        url: '',
        isEnable: 0
      },
      page: '',
      clearEdit: false,
      pageArr: [],
      rules: {
        projectId: [{ required: true, message: '请选择分类', trigger: 'blur' }],
        url: [{ required: true, message: '请上传图片', trigger: 'blur' }],
        name: [{ required: true, message: '请输入资源名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        content: [{ required: false, message: '请输入项目详情', trigger: 'blur' }]
      }
    }
  },
  methods: {
    open(page, data, row, pageArr) {
      this.pageArr = pageArr
      this.dialogVisible = true
      this.page = page
      this.clearEdit = true
      if (page === 'add') {
        this.title = '新增业务'
        this.list = data
      } else {
        this.title = '编辑业务'
        this.list = data
        this.ruleForm = row
      }
    },
    callbackChangeEditor(value) {
      console.log(value, '000')
      this.ruleForm.content = value
    },
    handleClose() {
      this.dialogVisible = false
      this.ruleForm = {}
      this.clearEdit = false
      this.$refs.ruleForm.resetFields()
      this.$emit('ok')
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.url = res.data.fileName
      this.ruleForm.imagesUrl = res.data.previewUrl + '/' + res.data.fileName
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isJPG) {
        this.$message.error('图片只能是JPG或者PNG 格式')
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },
    changeProject(e) {
      this.ruleForm.projectName = this.list.find(item => {
        return item.id === e
      }).name
    },
    getApi(api, data, msg) {
      REQUEST_LIST.REQUST_FCN(api, data, true).then(res => {
        if (res.status === 200) {
          this.$emit('ok')
          this.$message.success(msg)
          this.handleClose()
        }
      })
    },
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // this.ruleForm.content = this.$refs.editor.editorContent
          if (!this.ruleForm.content) {
            this.$message.error('请输入项目详情')
          } else {
            if (this.page === 'add') {
              this.ruleForm.projectParentId = this.pageArr[0].id
              this.getApi('RESOURCESADD', this.ruleForm, '添加成功！')
            } else {
              // this.ruleForm.content = this.$refs.editor.editorContent
              const data = {
                id: this.ruleForm.id,
                name: this.ruleForm.name,
                content: this.ruleForm.content,
                projectId: this.ruleForm.projectId,
                url: this.ruleForm.url,
                projectName: this.ruleForm.projectName,
                sort: this.ruleForm.sort,
                projectParentId: this.pageArr[0].id
              }
              this.getApi('RESOURCESEDIT', data, '编辑成功！')
            }
          }
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.handleClose()
      this.$emit('ok')
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
